package com.example.mo.Entity;

import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.util.SaFoxUtil;

import java.io.Serializable;
import java.util.*;

public class SaResult extends LinkedHashMap<String, Object> implements Serializable {
    private static final long serialVersionUID = 1L;
    public static final int CODE_SUCCESS = 200;
    public static final int CODE_ERROR = 500;

    public SaResult() {
    }

    public SaResult(int code, String msg, Object data) {
        if (data == null) {
            data = new HashMap<>();
        }
        this.setCode(code);
        this.setMsg(msg);
        this.setData(data);
    }

    public SaResult(Map<String, ?> map) {
        this.setMap(map);
    }

    public Integer getCode() {
        return (Integer) this.get("code");
    }

    public String getMsg() {
        return (String) this.get("msg");
    }

    public Object getData() {
        return this.get("data");
    }

    public SaResult setCode(int code) {
        this.put("code", code);
        return this;
    }

    public SaResult setMsg(String msg) {
        this.put("msg", msg);
        return this;
    }

    public SaResult setData(Object data) {
        if (data == null) {
            data = new HashMap<>();
        }
        this.put("data", data);
        return this;
    }

    public SaResult set(String key, Object data) {
        this.put(key, data);
        return this;
    }

    public <T> T get(String key, Class<T> cs) {
        return SaFoxUtil.getValueByType(this.get(key), cs);
    }

    public SaResult setMap(Map<String, ?> map) {
        for (String key : map.keySet()) {
            this.put(key, map.get(key));
        }
        return this;
    }

    public SaResult setJsonString(String jsonString) {
        Map<String, Object> map = SaManager.getSaJsonTemplate().jsonToMap(jsonString);
        return this.setMap(map);
    }

    public SaResult removeDefaultFields() {
        this.remove("code");
        this.remove("msg");
        this.remove("data");
        return this;
    }

    public SaResult removeNonDefaultFields() {
        // 使用增强 for 循环提高可读性
        for (String key : new ArrayList<>(this.keySet())) {
            if (!"code".equals(key) && !"msg".equals(key) && !"data".equals(key)) {
                this.remove(key);
            }
        }
        return this;
    }

    public static SaResult ok() {
        return new SaResult(200, "ok", new HashMap<>());
    }

    public static SaResult ok(String msg) {
        return new SaResult(200, msg, new HashMap<>());
    }

    public static SaResult data(Object data) {
        // 处理 data 为 null 的情况，使用空 Map 替代
        if (data == null) {
            data = new HashMap<>();
        }
        return new SaResult(200, "ok", data);
    }

    public static SaResult error() {
        return new SaResult(500, "error", new HashMap<>());
    }

    public static SaResult error(String msg) {
        return new SaResult(500, msg, new HashMap<>());
    }

    public static SaResult get(int code, String msg, Object data) {
        // 处理 data 为 null 的情况，使用空 Map 替代
        if (data == null) {
            data = new HashMap<>();
        }
        return new SaResult(code, msg, data);
    }

    public static SaResult empty() {
        return new SaResult();
    }

    public String toString() {
        return "{\"code\": " + this.getCode() + ", \"msg\": " + transValue(this.getMsg()) + ", \"data\": " + transValue(this.getData()) + "}";
    }

    private String transValue(Object value) {
        if (value == null) {
            return null;
        } else {
            return value instanceof String ? "\"" + value + "\"" : String.valueOf(value);
        }
    }

    public static SaResult code(int code) {
        return new SaResult(code, (String) null, new HashMap<>());
    }
}